package com.credithc.customers.manager.service.impl;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.exception.BsException;
import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.factory.DictionaryFactory;
import com.credithc.customers.base.mapper.CustomerRelationInfoMapper;
import com.credithc.customers.base.po.CustomerIdInfoPo;
import com.credithc.customers.base.po.CustomerRelationInfoPo;
import com.credithc.customers.base.ro.CustomerRelationInfoRo;
import com.credithc.customers.base.service.ICacheService;
import com.credithc.customers.base.vo.CustomerRelationInfoVo;
import com.credithc.customers.manager.service.CustomerIdInfoService;
import com.credithc.customers.manager.service.CustomerRelationInfoService;
import com.credithc.customers.manager.vo.EChartsRVo;
import com.credithc.customers.manager.vo.Link;
import com.credithc.customers.manager.vo.Node;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhq
 * @date 2018/12/14
 */
@Service
public class CustomerRelationInfoServiceImpl extends BaseServiceImpl<CustomerRelationInfoPo, CustomerRelationInfoRo, CustomerRelationInfoVo> implements CustomerRelationInfoService {
    @Autowired
    CustomerRelationInfoMapper customerRelationInfoMapper;
    @Autowired
    CustomerIdInfoService customerIdInfoService;
    @Autowired
    CustomerRelationInfoService customerRelationInfoService;
    @Autowired
    private ICacheService cacheService;

    @Override
    public PageInfo<CustomerRelationInfoVo> queryByCustomerRelationInfoRo(CustomerRelationInfoRo customerRelationInfoRo, Integer pageNo, Integer pageSize) {
        PageInfo<CustomerRelationInfoVo> pageInfo = new PageInfo<>();
        RowBounds rowBounds = new RowBounds(pageSize * (pageNo - 1), pageSize);

        Integer count = customerRelationInfoMapper.searchCount(customerRelationInfoRo);
        List<CustomerRelationInfoVo> customerRelationInfoVos = customerRelationInfoMapper.searchPage(customerRelationInfoRo, rowBounds);
        Map<String, String> dictMap = DictionaryFactory.Dict.CUSTOMER_ROLE.getDictMap();
        for (CustomerRelationInfoVo c : customerRelationInfoVos) {
            String s = dictMap.get(c.getRoleInRelation());
            c.setRoleInRelation(s);
            c.setSystemSign(cacheService.getSystemNameBySign(c.getSystemSign()));
            c.setSubSystemSign(cacheService.getSystemNameBySign(c.getSubSystemSign()));
        }
        pageInfo.setList(customerRelationInfoVos);
        pageInfo.setPageNo(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalCount(count);
        return pageInfo;
    }

    @Override
    public EChartsRVo getCustomerRelation(String customerNo) {
        PageInfo<CustomerRelationInfoPo> customerRelationInfoPoPageInfo = null;
        PageInfo<CustomerRelationInfoPo> cr = null;
        List<String> customerIds = new ArrayList<>();
        List<Node> nodes = new ArrayList<>();
        List<Link> links = new ArrayList<>();
        Map<String, String> noNameMap = new HashMap<>();
        Map<String, String> dictMap = DictionaryFactory.Dict.CUSTOMER_ROLE.getDictMap();

        try {
            Example example = new Example(CustomerRelationInfoPo.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(customerNo)) {
                criteria.andEqualTo("customerNo", customerNo);
                customerRelationInfoPoPageInfo = customerRelationInfoService.queryPageResultByExample(example, 1, Integer.MAX_VALUE);
                List<CustomerRelationInfoPo> list = customerRelationInfoPoPageInfo.getList();
                List<String> relationIds = new ArrayList<>();
                for (CustomerRelationInfoPo c : list) {
                    relationIds.add(c.getRelationId());
                }
                Example example1 = new Example(CustomerRelationInfoPo.class);
                Example.Criteria criteria1 = example1.createCriteria();
                if (relationIds.size() > 0) {
                    criteria1.andIn("relationId", relationIds);
                    cr = customerRelationInfoService.queryPageResultByExample(example1, 1, Integer.MAX_VALUE);
                }
                if (cr != null) {
                    List<CustomerRelationInfoPo> cp = cr.getList();

                    if (list.size() > 0) {
                        for (CustomerRelationInfoPo crp : cp) {
                            customerIds.add(crp.getCustomerNo());
                        }
                    }

                    Example example2 = new Example(CustomerIdInfoPo.class);
                    Example.Criteria criteria2 = example2.createCriteria();
                    if (customerIds.size() > 0) {
                        criteria2.andIn("customerNo", customerIds);
                        PageInfo<CustomerIdInfoPo> customerIdInfoPoPageInfo = customerIdInfoService.queryPageResultByExample(example2, 1, Integer.MAX_VALUE);
                        List<CustomerIdInfoPo> list1 = customerIdInfoPoPageInfo.getList();
                        if (list1.size() > 0) {
                            for (CustomerIdInfoPo ciifp : list1) {
                                noNameMap.put(ciifp.getCustomerNo(), ciifp.getCustomerName());
                            }
                        }
                    }
                    Node main = new Node();
                    main.setLabel(noNameMap.get(customerNo) == null ? "客户号:" + customerNo : noNameMap.get(customerNo));
                    main.setName(customerNo);
                    main.setCategory(0);
                    main.setValue(100);
                    nodes.add(main);
                    for (CustomerRelationInfoPo crip : cp) {

                        if (!customerNo.equals(crip.getCustomerNo())) {
                            Node node = new Node();
                            node.setName("关系：" + dictMap.get(Integer.toString(crip.getRoleInRelation())) + "    客户号：" + crip.getCustomerNo());
                            node.setLabel(noNameMap.get(crip.getCustomerNo()) == null ? "客户号：" + crip.getCustomerNo() : noNameMap.get(crip.getCustomerNo()));
                            node.setCategory(1);
                            nodes.add(node);
                            Link link = new Link();
                            link.setTarget(customerNo);
                            link.setSource("关系：" + dictMap.get(Integer.toString(crip.getRoleInRelation())) + "    客户号：" + crip.getCustomerNo());
                            link.setName(dictMap.get(Integer.toString(crip.getRoleInRelation())));
                            links.add(link);
                        }
                    }
                    // EChartsRelationVo eChartsRelationVo = new EChartsRelationVo("人物关系", nodes, links);

                    return new EChartsRVo(nodes, links);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }


        return null;

    }
}
   /* @Autowired
    ProductInfoMapper productInfoMapper;
    @Override
    public void test1(){
        String str = "1 => '工薪贷',2 => '精英贷',3 => '工薪恒楼贷',\n" +
                "301 => '工薪按揭恒楼贷',\n" +
                "4 => '助业恒楼贷',\n" +
                "302 => '助业按揭恒楼贷',\n" +
                "5 => '助业贷A',\n" +
                "6 => '助业贷B',\n" +
                "7 => '助业贷C',\n" +
                "12 => '社保贷',\n" +
                "13 => '保单贷（旧）',\n" +
                "300 => '保单贷A（旧）',\n" +
                "305 => '保单贷A',\n" +
                "306 => '保单贷B',\n" +
                "307 => '保单贷C',\n" +
                "308 => '保单贷D',\n" +
                "309 => '农银贷（受薪A）',\n" +
                "310 => '农银贷（受薪B）',\n" +
                "311 => '农银贷（助业A）',\n" +
                "312 => '农银贷（助业B）',\n" +
                "313 => '农银贷（种养殖）',\n" +
                "314 => '车主融',\n" +
                "315 => '恒银融',\n" +
                "316 => '精英融',\n" +
                "317 => '社保融',\n" +
                "318 => '保单融',\n" +
                "14 => '精英贷(信托)',\n" +
                "140 => '助业恒楼贷（信托）',\n" +
                "141 => '保单贷（信托）',\n" +
                "142 => '社保贷（信托）',\n" +
                "143 => '工薪贷(信托)',\n" +
                "144 => '工薪恒楼贷(信托)',\n" +
                "145 => '助业贷(信托)',\n" +
                "\n" +
                "\n" +
                "15 => '车主贷',\n" +
                "\n" +
                "\n" +
                "20 => '惠农贷',\n" +
                "21 => '恒农贷',\n" +
                "22 => '恒银贷',\n" +
                "60 => '散标',\n" +
                "1010 => '恒昌E贷',\n" +
                "\n" +
                "80 => '精英贷(渤海)',\n" +
                "202=>'工薪贷(集合标)',\n" +
                "208=>'精英贷(集合标)',\n" +
                "200=>'工薪恒楼贷(集合标)',\n" +
                "205=>'助业恒楼贷(集合标)',\n" +
                "203=>'社保贷(集合标)',\n" +
                "201=>'保单贷(集合标)',\n" +
                "204 => '助业贷A(集合标)',\n" +
                "211 => '助业贷B(集合标)',\n" +
                "212 => '助业贷C(集合标)',\n" +
                "210 => '车主贷(集合标)',\n" +
                "206 => '惠农贷(集合标)',\n" +
                "207 => '恒农贷(集合标)',\n" +
                "\n" +
                "\n" +

                "275=> '精英贷（华澳1）',\n" +
                "280 => '工薪贷（华澳1）',\n" +
                "281 => '工薪恒楼贷（华澳1）',\n" +
                "282=> '助业恒楼贷（华澳1）',\n" +
                "283 => '助业贷A（华澳1）',\n" +
                "284 => '助业贷B（华澳1）',\n" +
                "285 => '助业贷C（华澳1）',\n" +
                "286 => '社保贷（华澳1）',\n" +
                "287=> '保单贷（华澳1）',\n" +

                "260 =>'精英贷(外贸)',\n" +
                "265 =>'工薪贷(外贸)',\n" +
                "266 =>'工薪恒楼贷(外贸)',\n" +
                "267 =>'助业恒楼贷(外贸)',\n" +
                "268 =>'助业贷A(外贸)',\n" +
                "269 =>'助业贷B(外贸)',\n" +
                "270 =>'助业贷C(外贸)',\n" +
                "271 =>'社保贷(外贸)',\n" +
                "272 =>'保单贷(外贸)',\n" +

                "80 => '精英贷(渤海)',\n" +
                "252 =>'工薪贷(渤海)',\n" +
                "253 =>'工薪恒楼贷(渤海)',\n" +
                "254 =>'助业恒楼贷(渤海)',\n" +
                "255 =>'助业贷A(渤海)',\n" +
                "256 =>'助业贷B(渤海)',\n" +
                "257 =>'助业贷C(渤海)',\n" +
                "258 =>'社保贷(渤海)',\n" +
                "259 =>'保单贷(渤海)',\n" +

                "31401 => '车主融A',\n" +
                "31402 => '车主融B',\n" +
                "31403 => '车主融C',\n" +
                "31501 => '精英融A',\n" +
                "31502 => '精英融B',\n" +
                "31503 => '精英融C',\n" +
                "31601 => '社保融A',\n" +
                "31602 => '社保融B',\n" +
                "31603 => '社保融C',\n" +
                "31701 => '恒银融A',\n" +
                "31702 => '恒银融B',\n" +
                "31703 => '恒银融C',\n" +
                "31801 => '保单融A',\n" +
                "31802 => '保单融B',\n" +
                "31803 => '保单融C',\n" +

                "20101 => \"保单贷-融易贷A\",\n" +
                "20102 => \"保单贷-融易贷B\",\n" +
                "20103 => \"保单贷-融易贷C\",\n" +
                "20104 => \"恒银贷-融易贷A\",\n" +
                "20105 => \"恒银贷-融易贷B\",\n" +
                "20106 => \"恒银贷-融易贷C\",\n" +
                "20107 => \"精英贷-融易贷A\",\n" +
                "20108 => \"精英贷-融易贷B\",\n" +
                "20109 => \"精英贷-融易贷C\",\n" +
                "20110 => \"社金贷-融易贷A\",\n" +
                "20111 => \"社金贷-融易贷B\",\n" +
                "20112 => \"社金贷-融易贷C\",\n" +

                "511 => \"精英贷1\",\n" +
                "512 => \"精英贷2\",\n" +
                "513 => \"精英贷3\",\n" +
                "514 => \"精英贷4\",\n" +
                "515 => \"精英贷5\",\n" +
                "516 => \"精英贷6\",\n" +
                "517 => \"工薪贷1\",\n" +
                "518 => \"工薪贷2\",\n" +
                "519 => \"工薪贷3\",\n" +
                "520 => \"工薪贷4\",\n" +
                "521 => \"工薪贷5\",\n" +
                "522 => \"工薪贷6\",\n" +
                "523 => \"工薪按揭恒楼贷1\",\n" +
                "524 => \"工薪按揭恒楼贷2\",\n" +
                "525 => \"工薪按揭恒楼贷3\",\n" +
                "526 => \"工薪按揭恒楼贷4\",\n" +
                "527 => \"工薪按揭恒楼贷5\",\n" +
                "528 => \"工薪按揭恒楼贷6\",\n" +
                "529 => \"助业按揭恒楼贷1\",\n" +
                "530 => \"助业按揭恒楼贷2\",\n" +
                "531 => \"助业按揭恒楼贷3\",\n" +
                "532 => \"助业按揭恒楼贷4\",\n" +
                "533 => \"助业按揭恒楼贷5\",\n" +
                "534 => \"助业按揭恒楼贷6\",\n" +
                "535 => \"助业贷1\",\n" +
                "536 => \"助业贷2\",\n" +
                "537 => \"助业贷3\",\n" +
                "538 => \"助业贷4\",\n" +
                "539 => \"助业贷5\",\n" +
                "540 => \"助业贷6\",\n" +
                "541 => \"社金贷1\",\n" +
                "542 => \"社金贷2\",\n" +
                "543 => \"社金贷3\",\n" +
                "544 => \"社金贷4\",\n" +
                "545 => \"社金贷5\",\n" +
                "546 => \"社金贷6\",\n" +
                "547 => \"保单贷1\",\n" +
                "548 => \"保单贷2\",\n" +
                "549 => \"保单贷3\",\n" +
                "550 => \"保单贷4\",\n" +
                "551 => \"保单贷5\",\n" +
                "552 => \"保单贷6\",\n" +
                "553 => \"恒农贷1\",\n" +
                "554 => \"恒农贷2\",\n" +
                "555 => \"恒农贷3\",\n" +
                "556 => \"恒农贷4\",\n" +
                "557 => \"恒农贷5\",\n" +
                "558 => \"恒农贷6\",\n" +
                "559 => \"惠农贷1\",\n" +
                "560 => \"惠农贷2\",\n" +
                "561 => \"惠农贷3\",\n" +
                "562 => \"惠农贷4\",\n" +
                "563 => \"惠农贷5\",\n" +
                "564 => \"惠农贷6\",\n" +
                "565 => \"车主贷1\",\n" +
                "566 => \"车主贷2\",\n" +
                "567 => \"车主贷3\",\n" +
                "568 => \"车主贷4\",\n" +
                "569 => \"车主贷5\",\n" +
                "570 => \"车主贷6\",\n" +
                "571 => \"恒银贷1\",\n" +
                "572 => \"恒银贷2\",\n" +
                "573 => \"恒银贷3\",\n" +
                "574 => \"恒银贷4\",\n" +
                "575 => \"恒银贷5\",\n" +
                "576 => \"恒银贷6\",\n" +
                "577 => \"农银贷1\",\n" +
                "578 => \"农银贷2\",\n" +
                "579 => \"农银贷3\",\n" +
                "580 => \"农银贷4\",\n" +
                "581 => \"农银贷5\",\n" +
                "582 => \"农银贷6\",";
        String replace = str.replace("\n", "");

        String[] split = replace.split(",");
        for (int i =0;i<split.length;i++){
            String[] split1 = split[i].split("=>");
            // String r = split1[0]  + split1[1];
            String r = split1[0].replace(" ", "").replace("'", "").replace("\"", "");
            String r1 = split1[1].replace(" ", "").replace("'", "").replace("\"", "");
            ProductInfoPo productInfoPo = new ProductInfoPo();
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoPo.setProductNo(r);
            productInfoPo.setProductName(r1);
            productInfoPo.setProductType("0");
            productInfoPo.setProductChannel("个贷");
            productInfoPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
            productInfoPo.setSystemSign("HHR");
            productInfoPo.setSubSystemSign("HHR_JK");
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("DLR");
            productInfoPo.setSubSystemSign("DLR");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("XD");
            productInfoPo.setSubSystemSign("BJXD");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("XD");
            productInfoPo.setSubSystemSign("CCXD");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("XT");
            productInfoPo.setSubSystemSign("WMXT");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("XT");
            productInfoPo.setSubSystemSign("HAXT");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("XT");
            productInfoPo.setSubSystemSign("BHXT");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);
            productInfoPo.setSystemSign("CD");
            productInfoPo.setSubSystemSign("CD");
            productInfoPo.setId(UUIDUtils.getUUID());
            productInfoMapper.insert(productInfoPo);

        }
    }*/

